package com.leiqichao.backend.controller;

import com.leiqichao.backend.common.Result;
import com.leiqichao.backend.entity.Comment;
import com.leiqichao.backend.service.CommentService;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {
    private final CommentService commentService;
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @GetMapping("/article/{articleId}")
    public Result<List<Comment>> getCommentTree(@PathVariable Long articleId) {
        return Result.success(commentService.getCommentTree(articleId));
    }

    @PostMapping
    public Result<Comment> createComment(@RequestBody Comment comment) {
        return commentService.save(comment) ? 
            Result.success(comment) : Result.error(500, "评论提交失败");
    }

    @PostMapping("/approve") 
    public Result<?> approveComments(@RequestBody List<Long> commentIds) {
        return commentService.batchApprove(commentIds) ?
            Result.success() : Result.error(500, "审核操作失败");
    }

    @PostMapping("/mark-spam")
    public Result<?> markAsSpam(@RequestBody List<Long> commentIds) {
        return commentService.markAsSpam(commentIds) ?
            Result.success() : Result.error(500, "标记操作失败");
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteComment(@PathVariable Long id) {
        return commentService.removeById(id) ?
            Result.success() : Result.error(500, "删除失败");
    }
}